package compilers.compiler;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import compilers.exceptions.SemanticalException;
import compilers.exceptions.SyntacticalException;
import compilers.grammar.*;

import org.antlr.runtime.*;

public class Main {
	
	public static void main(String[] args) throws IOException {
	
			String inputFile = args[0];
			String outputFile = args[1];
			int visible = Integer.parseInt(args[2]);
		try {
			ANTLRFileStream input = new ANTLRFileStream(inputFile);	
			smallCGrammarLexer lexer = new smallCGrammarLexer(input);
			CommonTokenStream tokens = new CommonTokenStream(lexer);
			smallCGrammarParser parser = new smallCGrammarParser(tokens);
			Compiler compiler = parser.program();
			compiler.checkSyntax();
			compiler.checkSemantics();
			if (visible == 1) {
				System.out.println();
				compiler.printAST();
				System.out.println();
				System.out.println();
				compiler.printSymbolTable();
			}
			compiler.generatePcode(outputFile);
			System.out.println("");
			System.out.println("Compilition succesfull.");
		} catch (SyntacticalException e) {
			System.err.println(e.getMessage());
		} catch (SemanticalException e) {
			System.err.println(e.getMessage());
		} catch(Exception e) {
			System.err.println("Unexpected exception");
			e.printStackTrace();
		}
	}
}
